package namespace.jcu.mobile.library.system;

import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseUser;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.parse.ParseException;

public class LoginActivity extends Activity{

	EditText username;
	EditText password;
	Button login;

	public void onCreate(Bundle savedInstanceState) {
		Parse.initialize(this, "1GlGZQ1XWU34mHgFyDHZiZRr6HTTSWOZo2nSAowy", "GKVclKTd6TIPxLVPPbDiyvzAMoWA2XLf4ZdZYFwI");

        super.onCreate(savedInstanceState);
        

        setContentView(R.layout.login);
        
        username = (EditText) findViewById(R.id.loginUsernameTextbox);
        password = (EditText) findViewById(R.id.loginPasswordTextbox);
        login = (Button) findViewById(R.id.loginButton);
        
        login.setOnClickListener(new OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		ParseUser.logInInBackground(username.getText().toString(), password.getText().toString(), new LogInCallback() {
        			  public void done(ParseUser user, ParseException e) {
        			    if (user != null) {		//log in is successful
        			    	
                		Context context = getApplicationContext();
                		CharSequence text = "Log In is successful. Now Going Back To Home Page ...";
                		int duration = Toast.LENGTH_LONG;

                		
                		//toast message to display when the item is clicked
                		Toast toast = Toast.makeText(context, text, duration);
                		toast.show();
        			      Intent i = new Intent(getBaseContext(), HomeActivity.class);
        			      startActivity(i);
        			    } else {			//log in failed
                			
                    		Context context = getApplicationContext();
                    		CharSequence text = "Log In Failed. Please Try Again.";
                    		int duration = Toast.LENGTH_LONG;

                    		
                    		//toast message to display when the item is clicked
                    		Toast toast = Toast.makeText(context, text, duration);
                    		toast.show();
                    		
                    		//restart activity
                    		Intent intent = getIntent();
                    		finish();
                    		startActivity(intent);
        			    }
        			  }
        			});
        	}
        });
    }
        
}
